package org.linlinjava.litemall.admin.service;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import org.linlinjava.litemall.admin.util.AdminResponseCode;
import org.linlinjava.litemall.core.util.CharUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallRecharge;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.UserRechargeVo;
import org.linlinjava.litemall.db.service.LitemallRechargeService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import static org.linlinjava.litemall.admin.util.AdminResponseCode.ORDER_REFUND_FAILED;

/**
 * @description:
 * @author: liujun
 * @date: 2022-04-14 13:17
 */
@Service
public class AdminUserService {
    @Autowired
    private LitemallUserService userService;

    @Autowired
    private LitemallRechargeService rechargeService;

    @Autowired
    private LogHelper logHelper;

    @Transactional
    public Object userRecharge(@RequestBody UserRechargeVo user) {
        if (BigDecimal.ZERO.compareTo(user.getRechargeAmount()) >= 0
                || BigDecimal.ZERO.compareTo(user.getGiveAmount()) > 0) {
            return ResponseUtil.fail(AdminResponseCode.RECHARGE_BELOW_ZERO, "金额不能小于零");
        }
        //1、直接增加余额
        BigDecimal totalAmount = user.getRemainAmount().add(user.getRechargeAmount().add(user.getGiveAmount()));
        user.setRemainAmount(totalAmount);
        userService.updateById(user);
        //2、增加充值记录。充值类型：后台充值，
        this.addUserRecharge(user, totalAmount);
        //3、日志记录
        this.logRecharge(user);
        return ResponseUtil.ok();
    }

    private void logRecharge(@RequestBody UserRechargeVo user) {
        StringBuilder result = new StringBuilder();
        result.append(user.getMobile());
        result.append(" 充值 ");
        result.append(user.getRechargeAmount());
        if (BigDecimal.ZERO.compareTo(user.getRechargeAmount()) < 0) {
            result.append(" 赠送 ");
            result.append(user.getGiveAmount());
        }
        logHelper.logRechargeSucceed("后台充值", result.toString());
    }

    private void addUserRecharge(UserRechargeVo user, BigDecimal totalAmount) {
        String now = LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_PATTERN);
        String traceSn = now + CharUtil.getRandomNum(6);
        LitemallRecharge recharge = new LitemallRecharge();
        recharge.setUserId(user.getId());
        recharge.setTraceSn(traceSn);
        recharge.setMobile(user.getMobile());
        recharge.setRechargeAmount(user.getRechargeAmount());
        recharge.setGiveAmount(user.getGiveAmount());
        recharge.setTotalAmount(totalAmount);
        recharge.setRechargeWay("后台充值");
        rechargeService.add(recharge);
    }

    public Object addUser(LitemallUser user) {
        if (user.getUsername() == null || user.getMobile() == null) {
            return ResponseUtil.badArgument();
        }
        if (userService.checkByMobile(user.getMobile())) {
            return ResponseUtil.fail(AdminResponseCode.USER_NAME_EXIST, "手机号已存在");
        }
        user.setNickname(user.getUsername());
        userService.add(user);
        return ResponseUtil.ok(user);
    }
}
